<?php
##############################################
#
#   商品管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel;


class PrizerecordController extends AdminBaseController
{
    /**
     * 参与抽奖用户列表
     */
    public function PrizeList()
    {

        $mobile = I('mobile');
        $user_name = I('user_name');
        $type = I('prize_type');
        $starttime = strtotime(I('starttime'));
        $endtime = strtotime(I('endtime'));

        $data = D('prize_record') -> getUser($user_name,$mobile,$type,$starttime,$endtime);
        S('data',null);
        S('data',$data[2]);
        $this -> assign('user', $data[0]);
        $this -> assign('page', $data[1]);
        $this -> display('index');
    }

    /**
     * 删除
     */
    public function prizeDel()
    {
        if(!IS_AJAX) return;
        $result = D('prize_record')-> deleteUser(intval($_GET['record_id']));
        if($result)
            $this -> ajaxReturn(array('status' => 1,'message' => '删除成功'));
        else
            $this -> ajaxReturn(array('status' => 2,'message' => '操作失败'));
    }

    /**
     *全选删除
     * @Date 201703
     * @Author Linsenquan
     */
    public function deleteAllPriRe()
    {
        if(!IS_POST)
            $this->error('非法操作');
        //var_dump($_POST);die;
        $result = D('prize_record')->deleteAllPriRe($_POST);
        if($result)
            $this->success('删除成功',U('PrizeList'),1);
        else
            $this->error('删除失败','',2);
    }



    public function export()
    {
        $mobile = I('get.mobile');
        //$row = M('prize_record')->field('user_id,user_name,mobile,prize_id,prize_type,time')->select();
        //var_dump($row);die;
        $rows = S('data');
        //var_dump($row[0]);DIE;
        $row = $rows[2];

        foreach ($row as $k => $v)
        {
            if($v['prize_type'] == 1)
            {
                $row[$k]['prize_type'] = '抵用券';
            }
            else
            {
                $row[$k]['prize_type'] = '实物';
            }
            $row[$k]['prize_id'] = M('prize') ->field('prize_name') ->where(array('p_id'=>$v['prize_id']))-> find();

            $row[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
        }
        //var_dump($row);die;

        header("Content-type:application/vnd.ms-excel");
        header("Accept-Ranges:bytes");
        header("Content-Disposition:filename=".time().".xls");
        header("Pragma: no-cache");
        echo '
    <html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head>
    <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <!--[if gte mso 9]><xml>
    <x:ExcelWorkbook>
    <x:ExcelWorksheets>
    <x:ExcelWorksheet>
    <x:Name></x:Name>
    <x:WorksheetOptions>
    <x:DisplayGridlines/>
    </x:WorksheetOptions>
    </x:ExcelWorksheet>
    </x:ExcelWorksheets>
    </x:ExcelWorkbook>
    </xml><![endif]-->
    </head>
';

        echo '<table>';
        echo '<tr>';
        echo '<td style="background-color:red;">用户ID</td>';
        echo '<td style="background-color:red;">姓名</td>';
        echo '<td style="background-color:red;">电话</td>';
        echo '<td style="background-color:red;">奖品名称</td>';
        echo '<td style="background-color:red;">奖品类型</td>';
        echo '<td style="background-color:red;">中奖时间</td>';
        echo '</tr>';

        //$row=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);

        $i=0;
        while ($i<count($row))
        {
            echo '<tr>';
            echo "<td align='center' style='vnd.ms-excel.numberformat:@'>".$row[$i]['user_id']."</td>";
            echo "<td align='center'>".$row[$i]['user_name']."</td>";
            echo "<td align='center'>".$row[$i]['mobile']."</td>";
            echo "<td align='center'>".$row[$i]['prize_id']['prize_name']."</td>";
            echo "<td align='center'>".$row[$i]['prize_type']."</td>";
            echo "<td align='center'>".$row[$i]['time']."</td>";
            echo '</tr>';
            $i++;
        }
        echo '</table>';
    }


    /**
     *全部导出
     * @Date 201703
     * @Author Linsenquan
     */
    public function allExport()
    {
        $row = M('prize_record')->field('user_id,user_name,mobile,prize_id,prize_type,time')->select();
        //var_dump($row);die;
            foreach ($row as $k => $v)
            {
                if($v['prize_type'] == 1)
                {
                    $row[$k]['prize_type'] = '抵用券';
                }
                else
                {
                    $row[$k]['prize_type'] = '实物';
                }
                $row[$k]['prize_id'] = M('prize') ->field('prize_name') ->where(array('p_id'=>$v['prize_id']))-> find();

                $row[$k]['time'] = date('Y-m-d H:i:s',$v['time']);
            }
        //var_dump($row);die;

        header("Content-type:application/vnd.ms-excel");
        header("Accept-Ranges:bytes");
        header("Content-Disposition:filename=".time().".xls");
        header("Pragma: no-cache");
        echo '
    <html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head>
    <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <!--[if gte mso 9]><xml>
    <x:ExcelWorkbook>
    <x:ExcelWorksheets>
    <x:ExcelWorksheet>
    <x:Name></x:Name>
    <x:WorksheetOptions>
    <x:DisplayGridlines/>
    </x:WorksheetOptions>
    </x:ExcelWorksheet>
    </x:ExcelWorksheets>
    </x:ExcelWorkbook>
    </xml><![endif]-->
    </head>
';

        echo '<table>';
        echo '<tr>';
        echo '<td style="background-color:red;">用户ID</td>';
        echo '<td style="background-color:red;">姓名</td>';
        echo '<td style="background-color:red;">电话</td>';
        echo '<td style="background-color:red;">奖品名称</td>';
        echo '<td style="background-color:red;">奖品类型</td>';
        echo '<td style="background-color:red;">中奖时间</td>';
        echo '</tr>';

        //$row=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);

        $i=0;
        while ($i<count($row))
        {
            echo '<tr>';
            echo "<td align='center' style='vnd.ms-excel.numberformat:@'>".$row[$i]['user_id']."</td>";
            echo "<td align='center'>".$row[$i]['user_name']."</td>";
            echo "<td align='center'>".$row[$i]['mobile']."</td>";
            echo "<td align='center'>".$row[$i]['prize_id']['prize_name']."</td>";
            echo "<td align='center'>".$row[$i]['prize_type']."</td>";
            echo "<td align='center'>".$row[$i]['time']."</td>";
            echo '</tr>';
            $i++;
        }
        echo '</table>';
    }


}




